package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.Classification;
import cn.stylefeng.guns.modular.system.entity.ProductModel;
import cn.stylefeng.guns.modular.system.model.params.ProductModelParam;
import cn.stylefeng.guns.modular.system.service.ClassificationService;
import cn.stylefeng.guns.modular.system.service.ProductModelService;
import cn.stylefeng.guns.modular.system.service.impl.ProductModelServiceImpl;
import cn.stylefeng.guns.sys.modular.system.controller.SystemController;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 产品型号表控制器
 *
 * @author 小飞
 * @Date 2019-06-27 18:44:45
 */
@Controller
@RequestMapping("/productModel")
public class ProductModelController extends BaseController {

    private String PREFIX = "/productModel";

    @Autowired
    private ProductModelService productModelService;
    @Autowired
    private ClassificationService classificationService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/productModel.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/productModel_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/productModel_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ProductModelParam productModelParam) {

        Long kind = productModelParam.getKindId();
        if (classificationService.getById(kind).getPid() != 0L){
            return  ResponseData.error(201,"请选择一级分类！");
        }
        //自动生成到分类表
        Classification classification = classificationService.getOne(new QueryWrapper<Classification>()
        .eq("name",productModelParam.getModelNameIn()));

        if (ToolUtil.isNotEmpty(classification)){
            return  ResponseData.error(201,"已经存在该型号");
        }else {
            Classification classificationInsert = new  Classification();
            classificationInsert.setPid(productModelParam.getKindId());
            classificationInsert.setName(productModelParam.getModelNameIn());
            classificationService.save(classificationInsert);

            //生成型号表
            Classification classificationLast = classificationService.getOne(new QueryWrapper<Classification>()
                    .eq("name",productModelParam.getModelNameIn()));
            productModelParam.setKindId(classificationLast.getId());
            ProductModel productModel = new  ProductModelServiceImpl().getEntity(productModelParam);
            productModelService.getBaseMapper().insert(productModel);
            return ResponseData.success();
        }
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ProductModelParam productModelParam) {
        this.productModelService.update(productModelParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ProductModelParam productModelParam) {

        //删除分类
        ProductModel productModel = productModelService.getById(productModelParam.getId());
        classificationService.getBaseMapper().deleteById(productModel.getKindId());

        //删除型号
        this.productModelService.delete(productModelParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ProductModelParam productModelParam) {
        ProductModel detail = this.productModelService.getById(productModelParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ProductModelParam productModelParam) {
        return this.productModelService.findPageBySpec(productModelParam);
    }

}


